/*********************************************************************
PROGRAM: WMKM_REStat_nhis_rdc.do
PURPOSE: Estimates mobility from state of birth and estimates state-
		 level models using restricted access NHIS data for 
		 Wherry, Miller, Meyer, and Kaestner
DATE:	 February 3, 2017
NOTES:	 This analysis uses restricted access NHIS data for years
		 1992-2014 with state of birth and current state of residence.
		 To run this code, put source NHIS files in a subdirectory
		 called src and create subdirectories data and output for 
		 created data files and regression output. 
CONTACT: Laura Wherry, lwherry@mednet.ucla.edu
**********************************************************************/
/*********************************************************************
A) EXAMINE MOBILITY FROM STATE OF BIRTH
*********************************************************************/
use src/nhisrestricted, clear

*create sample
rename dob_m birthmo
rename dob_y_p birthyr
destring birthyr birthmo, replace
keep if inrange(birthyr,1979,1987)
drop if birthyr==1979 & inrange(birthmo,1,9)
drop if birthyr==1987 & inrange(birthmo,10,12)
destring plborn state, replace
drop if plborn==. | state==. 
drop if plborn>56 | inlist(plborn,3,7,14,43,52)

*create outcome
gen mover=1*(plborn!=state)

**race
replace racerec=racerecr if srvy_yr==1999
replace racerec=racrec_i if inrange(srvy_yr,2000,2002)
replace racerec=racreci2 if inrange(srvy_yr,2003,2005)
replace racerec=racreci3 if inrange(srvy_yr,2006,2014)
recode racerec (2=1) (1=0) (3/4=0), gen(black)
label var black "Race = black"

*create regression vars
sort birthyr birthmo
egen num=group(birthyr birthmo)
gen c=num-49
gen post=c>=0
gen c2=c^2
gen postc=c*post
gen postc2=c2*post

gen win3=1*(inrange(c,-36,35))
gen win2=1*(inrange(c,-24,23))

*code up state discontinuities
recode plborn (23=0.030) (33=0.178) (50=0.060) (25=0.082) ///
(44=0.332) (9=0.382) (36=0.132) (34=0.208) (42=0.285) ///
(39=0.502) (18=0.696) (17=0.406) (26=0.194) (55=0.197) ///
(27=0.196) (19=0.394) (29=0.298) (38=0.202) (46=1.152) ///
(31=0.515) (20=0.354) (10=0.103) (24=0.881) (11=0.293) ///
(51=0.254) (54=0.750) (37=0.150) (45=0.234) (13=0.145) ///
(12=0.644) (21=0.587) (47=0.463) (1=0.801) (28=0.481) ///
(5=1.035) (22=1.002) (40=0.543) (48=1.045) (30=1.214) ///
(16=1.014) (56=0.710) (8=0.707) (35=0.955) (4=0.794) ///
(49=0.157) (32=0.545) (53=0.168) (41=0.388) (6=0.046) ///
(2=0.225) (15=0.259), gen(discontnb)
recode plborn (23=0.017) (33=1.647) (50=0.0189) (25=0.195) (44=0.342) ///
(9=0.679) (36=0.186) (34=0.559) (42=0.519) (39=0.874) ///
(18=0.995) (17=0.595) (26=0.298) (55=0.625) (27=0.114) ///
(19=0.602) (29=0.422) (38=0.016) (46=2.668) (31=1.161) ///
(20=0.220) (10=0.127) (24=0.1682) (11=0.677) (51=0.368) ///
(54=0.784) (37=0.268) (45=1.066) (13=0.283) (12=1.463) ///
(21=0.777) (47=0.628) (1=2.314) (28=1.612) (5=2.308) ///
(22=2.398) (40=1.072) (48=1.295) (30=0.218) (16=0.067) ///
(56=0.296) (8=0.879) (35=0.364) (4=0.027) (49=1.791) ///
(32=0.207) (53=0.132) (41=0.092) (6=0.092) (2=0.314) (15=0.351), gen(discontb)
recode plborn (23=0.030) (33=0.180) (50=0.060) (25=0.089) ///
(44=0.333) (9=0.417) (36=0.141) (34=0.264) (42=0.311) ///
(39=0.548) (18=0.724) (17=0.444) (26=0.212) (55=0.228) ///
(27=0.194) (19=0.399) (29=0.313) (38=0.201) (46=1.155) ///
(31=0.545) (20=0.344) (10=0.108) (24=1.096) (11=0.620) ///
(51=0.281) (54=0.751) (37=0.180) (45=0.567) (13=0.193) ///
(12=0.853) (21=0.603) (47=0.501) (1=1.311) (28=1.002) ///
(5=1.328) (22=1.509) (40=0.590) (48=1.077) (30=1.211) ///
(16=1.008) (56=0.707) (8=0.716) (35=0.939) (4=0.811) ///
(49=0.156) (32=0.689) (53=0.170) (41=0.379) (6=0.050) ///
(2=0.229) (15=0.261), gen(discontall)
gen postdiscontb=discontb*post
gen postdiscontnb=discontnb*post
gen postdiscontall=discontall*post
drop if state>56 | inlist(state,3,7,14,43,52)
drop if plborn>56 | inlist(plborn,3,7,14,43,52)

*summary stats
mean mover if c<0
mean mover if c<0 & black==1
mean mover if c<0 & black==0
save data/moverrd, replace

/********************* Mobility at age 25 ***********************/
use data/moverrd, clear
keep if age==25
mean mover if c<0
mean mover if c<0 & black==1
mean mover if c<0 & black==0

***estimates from these models reported in Appendix Table 22
di "All RACES, mover"
regress mover post c c2 postc postc2 i.birthmo, cluster(c)
outreg2 using output/moverrd, replace stats(coef ci) excel bdec(3)
regress mover post c c2 postc postc2 i.birthmo if win3, cluster(c)
outreg2 using output/moverrd, stats(coef ci) excel bdec(3)
regress mover post c c2 postc postc2 i.birthmo if win2, cluster(c)
outreg2 using output/moverrd, stats(coef ci) excel bdec(3)
qui: regress mover i.birthmo, robust
predict ypub, xb
gen epub=mover-ypub
rdrobust epub c, bwselect(IK)
outreg2 using output/moverrd, stats(coef ci) excel bdec(3)
rdrobust epub c
outreg2 using output/moverrd, stats(coef ci) excel bdec(3)
drop ypub 
drop epub

di "BLACKS, mover"
regress mover post c c2 postc postc2 i.birthmo if black==1, cluster(c)
outreg2 using output/moverrd, stats(coef ci) excel bdec(3)
regress mover post c c2 postc postc2 i.birthmo if win3 & black==1, cluster(c)
outreg2 using output/moverrd, stats(coef ci) excel bdec(3)
regress mover post c c2 postc postc2 i.birthmo if win2 & black==1, cluster(c)
outreg2 using output/moverrd, stats(coef ci) excel bdec(3)
qui: regress mover i.birthmo if black==1, robust
predict ypub if black==1, xb
gen epub=mover-ypub if black==1
rdrobust epub c, bwselect(IK)
outreg2 using output/moverrd, stats(coef ci) excel bdec(3)
rdrobust epub c
outreg2 using output/moverrd, stats(coef ci) excel bdec(3)
drop ypub 
drop epub

di "NON-BLACKS, mover"
regress mover post c c2 postc postc2 i.birthmo if black==0, cluster(c)
outreg2 using output/moverrd, stats(coef ci) excel bdec(3)
regress mover post c c2 postc postc2 i.birthmo if win3 & black==0, cluster(c)
outreg2 using output/moverrd, stats(coef ci) excel bdec(3)
regress mover post c c2 postc postc2 i.birthmo if win2 & black==0, cluster(c)
outreg2 using output/moverrd, stats(coef ci) excel bdec(3)
qui: regress mover i.birthmo if black==0, robust
predict ypub if black==0, xb
gen epub=mover-ypub if black==0
rdrobust epub c, bwselect(IK)
outreg2 using output/moverrd, stats(coef ci) excel bdec(3)
rdrobust epub c
outreg2 using output/moverrd, stats(coef ci) excel bdec(3)
drop ypub 
drop epub

**state-level mobility analysis, reported in Appendix Table 12
!rm output/moverrdstate*
reg mover postdiscontall post post#c.c post#c.c2 c c2 i.plborn i.birthmo, cluster(plborn)	
outreg2 postdiscontall using output/moverrdstate, append bdec(3) excel 
reg mover postdiscontb post post#c.c post#c.c2 c c2 i.plborn i.birthmo if black==1, cluster(plborn)
outreg2 postdiscontb using output/moverrdstate, append bdec(3) excel 
reg mover postdiscontnb post post#c.c post#c.c2 c c2 i.plborn i.birthmo if black==0, cluster(plborn)
outreg2 postdiscontnb using output/moverrdstate, append bdec(3) excel


/********************* Mobility in 2009 ***********************/
**estimtes mentioned in text, not reported in paper
use $tempdata/moverrd, clear
keep if srvy_yr==2009

mean mover if c<0
mean mover if c<0 & black==1
mean mover if c<0 & black==0

di "All RACES, mover"
regress mover post c c2 postc postc2 i.birthmo, cluster(c)
outreg2 using output/moverrd2, replace stats(coef ci) excel bdec(3)
regress mover post c c2 postc postc2 i.birthmo if win3, cluster(c)
outreg2 using output/moverrd2, stats(coef ci) excel bdec(3)
regress mover post c c2 postc postc2 i.birthmo if win2, cluster(c)
outreg2 using output/moverrd2, stats(coef ci) excel bdec(3)
qui: regress mover i.birthmo, robust
predict ypub, xb
gen epub=mover-ypub
rdrobust epub c, bwselect(IK)
outreg2 using output/moverrd2, stats(coef ci) excel bdec(3)
rdrobust epub c
outreg2 using output/moverrd2, stats(coef ci) excel bdec(3)
drop ypub 
drop epub

di "BLACKS, mover"
regress mover post c c2 postc postc2 i.birthmo if black==1, cluster(c)
outreg2 using output/moverrd2, stats(coef ci) excel bdec(3)
regress mover post c c2 postc postc2 i.birthmo if win3 & black==1, cluster(c)
outreg2 using output/moverrd2, stats(coef ci) excel bdec(3)
regress mover post c c2 postc postc2 i.birthmo if win2 & black==1, cluster(c)
outreg2 using output/moverrd2, stats(coef ci) excel bdec(3)
qui: regress mover i.birthmo if black==1, robust
predict ypub if black==1, xb
gen epub=mover-ypub if black==1
rdrobust epub c, bwselect(IK)
outreg2 using output/moverrd2, stats(coef ci) excel bdec(3)
rdrobust epub c
outreg2 using output/moverrd2, stats(coef ci) excel bdec(3)
drop ypub 
drop epub

di "NON-BLACKS, mover"
regress mover post c c2 postc postc2 i.birthmo if black==0, cluster(c)
outreg2 using output/moverrd2, stats(coef ci) excel bdec(3)
regress mover post c c2 postc postc2 i.birthmo if win3 & black==0, cluster(c)
outreg2 using output/moverrd2, stats(coef ci) excel bdec(3)
regress mover post c c2 postc postc2 i.birthmo if win2 & black==0, cluster(c)
outreg2 using output/moverrd2, stats(coef ci) excel bdec(3)
qui: regress mover i.birthmo if black==0, robust
predict ypub if black==0, xb
gen epub=mover-ypub if black==0
rdrobust epub c, bwselect(IK)
outreg2 using output/moverrd2, stats(coef ci) excel bdec(3)
rdrobust epub c
outreg2 using output/moverrd2, stats(coef ci) excel bdec(3)
drop ypub 
drop epub

**state-level mobility analysis
reg mover postdiscontall post post#c.c post#c.c2 c c2 i.plborn i.birthmo, cluster(plborn)	
outreg2 postdiscontall using output/moverrdstate, append bdec(3) excel 
reg mover postdiscontb post post#c.c post#c.c2 c c2 i.plborn i.birthmo if black==1, cluster(plborn)
outreg2 postdiscontb using output/moverrdstate, append bdec(3) excel 
reg mover postdiscontnb post post#c.c post#c.c2 c c2 i.plborn i.birthmo if black==0, cluster(plborn)
outreg2 postdiscontnb using output/moverrdstate, append bdec(3) excel

/*********************************************************************
B) EXAMINE HEALTH INSURANCE COVERAGE USING STATE-LEVEL DISCONTINUITIES
*********************************************************************/
forval y=1992/1996{
	local p=`y'-1900
	use src/nhisrestricted`y', clear

	*recode health ins vars, note: slightly different in 1992
	*to be consistent across years, public and coverage vars
	*take on a value of zero if no coverage or coverage status unknown
	if `y'==1992{
		gen coverage=1*(phlthinr==1)
		replace coverage=1 if hlthinsr==1
		replace coverage=1 if inrange(pubassr,1,2)
		recode medicaid (2=0) (7/9=.)
	}
	if inrange(`y',1993,1996){
		recode covstatr (2/3=0), gen(coverage)
		capture drop medicaid
		if inrange(`y',1993,1994){
			recode maidlmo (2=0) (7/9=.), gen(medicaid)
		}
		if inrange(`y',1995,1996){
			recode maidlamo (2=0) (7/9=.), gen(medicaid)
		}
	}
	label var coverage "Health insurance coverage status"
	label var medicaid "Medicaid coverage"	

	*recode race
	capture drop black
	gen black=1*(race==2)
	replace black=. if race==99

	*restrict sample
	destring dob_m dob_y_p, replace
	drop if dob_m==99 | dob_y_p==9999

	*keep variables need for study of insurance coverage
	keep age black wtfa dob_m dob_y_p coverage ///
	 medicaid state 

	gen svyyr=`y'
	su 
	save data/nhishi`y', replace
}


***prepare data for analysis
use data/nhishi1992, clear
forval y=1993/1996{
	append using data/nhishi`y'
}
rename dob_m birthmo
rename dob_y_p birthyr
keep if inrange(birthyr,1979,1987)
drop if birthyr==1979 & inrange(birthmo,1,9)
drop if birthyr==1987 & inrange(birthmo,10,12)

*create regression vars
sort birthyr birthmo
egen num=group(birthyr birthmo)
gen c=num-49
gen post=c>=0
gen c2=c^2
gen postc=c*post
gen postc2=c2*post

gen win3=1*(inrange(c,-36,35))
gen win2=1*(inrange(c,-24,23))

*code up state discontinuities
destring state, replace
recode state (23=0.030) (33=0.178) (50=0.060) (25=0.082) ///
(44=0.332) (9=0.382) (36=0.132) (34=0.208) (42=0.285) ///
(39=0.502) (18=0.696) (17=0.406) (26=0.194) (55=0.197) ///
(27=0.196) (19=0.394) (29=0.298) (38=0.202) (46=1.152) ///
(31=0.515) (20=0.354) (10=0.103) (24=0.881) (11=0.293) ///
(51=0.254) (54=0.750) (37=0.150) (45=0.234) (13=0.145) ///
(12=0.644) (21=0.587) (47=0.463) (1=0.801) (28=0.481) ///
(5=1.035) (22=1.002) (40=0.543) (48=1.045) (30=1.214) ///
(16=1.014) (56=0.710) (8=0.707) (35=0.955) (4=0.794) ///
(49=0.157) (32=0.545) (53=0.168) (41=0.388) (6=0.046) ///
(2=0.225) (15=0.259), gen(discontnb)
recode state (23=0.017) (33=1.647) (50=0.0189) (25=0.195) (44=0.342) ///
(9=0.679) (36=0.186) (34=0.559) (42=0.519) (39=0.874) ///
(18=0.995) (17=0.595) (26=0.298) (55=0.625) (27=0.114) ///
(19=0.602) (29=0.422) (38=0.016) (46=2.668) (31=1.161) ///
(20=0.220) (10=0.127) (24=0.1682) (11=0.677) (51=0.368) ///
(54=0.784) (37=0.268) (45=1.066) (13=0.283) (12=1.463) ///
(21=0.777) (47=0.628) (1=2.314) (28=1.612) (5=2.308) ///
(22=2.398) (40=1.072) (48=1.295) (30=0.218) (16=0.067) ///
(56=0.296) (8=0.879) (35=0.364) (4=0.027) (49=1.791) ///
(32=0.207) (53=0.132) (41=0.092) (6=0.092) (2=0.314) (15=0.351), gen(discontb)
recode state (23=0.030) (33=0.180) (50=0.060) (25=0.089) ///
(44=0.333) (9=0.417) (36=0.141) (34=0.264) (42=0.311) ///
(39=0.548) (18=0.724) (17=0.444) (26=0.212) (55=0.228) ///
(27=0.194) (19=0.399) (29=0.313) (38=0.201) (46=1.155) ///
(31=0.545) (20=0.344) (10=0.108) (24=1.096) (11=0.620) ///
(51=0.281) (54=0.751) (37=0.180) (45=0.567) (13=0.193) ///
(12=0.853) (21=0.603) (47=0.501) (1=1.311) (28=1.002) ///
(5=1.328) (22=1.509) (40=0.590) (48=1.077) (30=1.211) ///
(16=1.008) (56=0.707) (8=0.716) (35=0.939) (4=0.811) ///
(49=0.156) (32=0.689) (53=0.170) (41=0.379) (6=0.050) ///
(2=0.229) (15=0.261), gen(discontall)
gen postdiscontb=discontb*post
gen postdiscontnb=discontnb*post
gen postdiscontall=discontall*post
drop if state>56 | inlist(state,3,7,14,43,52)

save data/rdnhis_clean, replace


****state-level analysis, reported in Appendix Table 11
use data/rdnhis_clean, clear
!rm output/nhisstate*
foreach x of var coverage medicaid {
	reg `x' postdiscontall post post#c.c post#c.c2 c c2 i.state i.birthmo, cluster(state)	
	outreg2 postdiscontall using output/nhisstate, append bdec(3) excel 
	reg `x' postdiscontb post post#c.c post#c.c2 c c2 i.state i.birthmo if black==1, cluster(state)
	outreg2 postdiscontb using output/nhisstate, append bdec(3) excel 
	reg `x' postdiscontnb post post#c.c post#c.c2 c c2 i.state i.birthmo if black==0, cluster(state)
	outreg2 postdiscontnb using output/nhisstate, append bdec(3) excel
}





